package com.aixin.csopservice.service.impl;

import com.aixin.csopservice.domain.PointConTxnHist;
import com.aixin.csopservice.model.*;
import com.aixin.csopservice.repository.PointRepository;
import com.aixin.csopservice.service.PointService;
import com.aixin.csopservice.utils.RSA2Util;
import com.aixin.csopservice.utils.Util;
import com.aixin.csopuser.domain.CustomerDomain;
import com.aixin.csopuser.model.CustomerModel;
import com.aixin.csopuser.service.YjkApiService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class PointServiceImpl implements PointService {

    @Autowired
    PointRepository pointRepository;
    @Autowired
    private YjkApiService yjkApiService;

    @Override
    public CustomerPointModel getCustomerID(CustomerModel customerModel) {
        CustomerPointModel customerPointModel=new CustomerPointModel();
        if(StringUtils.isEmpty(customerModel.getCertID()) )
        {
            return customerPointModel;
        }
        if(customerModel.getCertType()==null )
        {
            return  customerPointModel;
        }
        if(StringUtils.isEmpty(customerModel.getPhoneNumber()) )
        {
            return  customerPointModel;
        }
        if(StringUtils.isEmpty(customerModel.getBirthday()) )
        {
            return customerPointModel;
        }
        if(StringUtils.isEmpty(customerModel.getCustName()) )
        {
            return customerPointModel;
        }
        List<CustomerDomain> domainList=yjkApiService.apiGetCustomer(customerModel);
        if (domainList.size()==0) {
            customerPointModel.setReturnResultFlag("FALSE");
            customerPointModel.setErrorMsg("鉴权失败,用户不存在!");
            return customerPointModel;
        }
        try{
            customerPointModel.setCustomerID(RSA2Util.encrypt(domainList.get(0).getCustomerID()));
            customerPointModel.setRequestTime(Util.getNowDate());
            customerPointModel.setReturnResultFlag("SUCCESS");
        }catch (Exception exception){
            customerPointModel.setReturnResultFlag("FALSE");
            customerPointModel.setErrorMsg("加密错误");
            return customerPointModel;
        }
        return customerPointModel;

    }

    @Override
    public ReturnPoint getPoint(CustomerPointModel customerPointModel) {
        ReturnPoint returnPoint = new ReturnPoint();
        if(null==customerPointModel.getCustomerID()&&!"".equals(customerPointModel.getCustomerID())){
            returnPoint.setAvailablePoints(0);
            returnPoint.setRequestTime(Util.getNowDate());
            returnPoint.setReturnResultFlag("FALSE");
            returnPoint.setErrorMsg("FALSE");
        }
        Map<String,Integer> customer = pointRepository.getPoint(customerPointModel.getCustomerID());
        if(customer==null||customer.size()==0){
            returnPoint.setAvailablePoints(0);
            returnPoint.setRequestTime(Util.getNowDate());
            returnPoint.setReturnResultFlag("FALSE");
            returnPoint.setErrorMsg("用户不存在");
            return returnPoint;
        }
        int point;
        if (customer.get("remnPoint")==null){
            point=0;
        }else{
            point= customer.get("remnPoint");
        }
        returnPoint.setAvailablePoints(point);
        returnPoint.setRequestTime(Util.getNowDate());
        returnPoint.setReturnResultFlag("SUCCESS");
        returnPoint.setErrorMsg("SUCCESS");
        return returnPoint;
    }

    @Override
    @Transactional
    public ReturnPointMessage placeAnOrder(PlaceAnOrderModel placeAnOrderModel) {
        ReturnPointMessage returnPointMessage = new ReturnPointMessage();
        if(null==placeAnOrderModel.getCustomerID()&&!"".equals(placeAnOrderModel.getCustomerID())){
            returnPointMessage.setRequestTime(Util.getNowDate());
            returnPointMessage.setReturnResultFlag("FALSE");
            returnPointMessage.setErrorMsg("用户不允许为NULL");
        }
        Map<String,Integer> customer = pointRepository.getPoint(placeAnOrderModel.getCustomerID());
        Map<String,Integer> usedCustomerPoint = pointRepository.getUsedPoint(placeAnOrderModel.getCustomerID());
        if(customer==null){
            returnPointMessage.setRequestTime(Util.getNowDate());
            returnPointMessage.setReturnResultFlag("FALSE");
            returnPointMessage.setErrorMsg("用户不存在");
            return returnPointMessage;
        }
        String incGenNum=pointRepository.getIncGenNumber(placeAnOrderModel.getCustomerID());
        if(StringUtils.isEmpty(incGenNum))
        {
            returnPointMessage.setRequestTime(Util.getNowDate());
            returnPointMessage.setReturnResultFlag("FALSE");
            returnPointMessage.setErrorMsg("未找到对应的加分信息");
        }
        int point=0;
        int usedPoint=0;
        if (customer.get("remnPoint")!=null){
            point=customer.get("remnPoint");
        }

        if(usedCustomerPoint.get("usedPoint")!=null)
        {
            usedPoint=usedCustomerPoint.get("usedPoint");
        }

        Instant nowDate = Instant.now();
        List<PointConTxnHist> pointConTxnHists = new ArrayList<>();
        int allPoint=0;
        for (OrderModel orderModel : placeAnOrderModel.getOrderList()) {
            PointConTxnHist pointConTxnHist = new PointConTxnHist();
            pointConTxnHist.setPointGenNumber(Util.getUUID());
            pointConTxnHist.setCustomerID(placeAnOrderModel.getCustomerID());
            pointConTxnHist.setOrderNumber(orderModel.getOrderNumber());
            pointConTxnHist.setPointStatus(1);
            pointConTxnHist.setPointType(1);
            pointConTxnHist.setTxnTime(placeAnOrderModel.getRequestTime());
            pointConTxnHist.setTxnContent(orderModel.getTxnContent());
            pointConTxnHist.setTxnPoint(orderModel.getTxnPoint());
            pointConTxnHist.setSystemDeduction(0);
            pointConTxnHist.setCreateTime(nowDate);
            pointConTxnHist.setPointInNum(incGenNum);
            pointConTxnHists.add(pointConTxnHist);
            allPoint=allPoint+orderModel.getTxnPoint();
        }
        if(point<allPoint){
            returnPointMessage.setRequestTime(Util.getNowDate());
            returnPointMessage.setReturnResultFlag("FALSE");
            returnPointMessage.setErrorMsg("可用积分超过剩余积分不允许被消耗");
            returnPointMessage.setCurrentPoint(point);
            return returnPointMessage;
        }
        point=point-allPoint;
        usedPoint=usedPoint+allPoint;
        pointRepository.updateCustPoint(point,usedPoint,placeAnOrderModel.getCustomerID());
        List list=pointRepository.saveAll(pointConTxnHists);
        returnPointMessage.setCurrentPoint(point);
        returnPointMessage.setReturnResultFlag("SUCCESS");
        returnPointMessage.setRequestTime(Util.getNowDate());
        returnPointMessage.setPointList(list);
        return returnPointMessage;
    }

    @Override
    @Transactional
    public ReturnChargebackModel chargeback(ChargebackModel chargebackModel) {
        ReturnChargebackModel returnPointMessage = new ReturnChargebackModel();
        if(null==chargebackModel.getCustomerID()&&!"".equals(chargebackModel.getCustomerID())){
            returnPointMessage.setRequestTime(Util.getNowDate());
            returnPointMessage.setReturnResultFlag("FALSE");
            returnPointMessage.setErrorMsg("用户不允许为NULL");
        }
        Map<String,Integer> customer = pointRepository.getPoint(chargebackModel.getCustomerID());
        Map<String,Integer> usedCustomerPoint = pointRepository.getUsedPoint(chargebackModel.getCustomerID());
        if(customer==null){
            returnPointMessage.setRequestTime(Util.getNowDate());
            returnPointMessage.setReturnResultFlag("FALSE");
            returnPointMessage.setErrorMsg("用户不存在");
            return returnPointMessage;
        }
        int point=0;
        int usedPoint=0;
        if (customer.get("remnPoint")!=null){
            point=customer.get("remnPoint");
        }
        if (usedCustomerPoint.get("usedPoint")!=null){
            usedPoint=usedCustomerPoint.get("usedPoint");
        }
        int txnPOInt=pointRepository.getTxnPoint(chargebackModel.getOrderNumber());
        int num = pointRepository.chargeback(chargebackModel.getOrderNumber());
        point=point+txnPOInt;
        usedPoint=usedPoint-txnPOInt;
        pointRepository.updateCustPoint(point,usedPoint,chargebackModel.getCustomerID());
        String nowDate = Util.getNowDate();
        returnPointMessage.setRequestTime(nowDate);
        returnPointMessage.setOrderNumber(chargebackModel.getOrderNumber());
        if (num > 0) {
            returnPointMessage.setReturnResultFlag("SUCCESS");
        } else {
            returnPointMessage.setReturnResultFlag("FALSE");
        }
        return returnPointMessage;
    }
}
